<template>
  <div class="app-container">
    <h2 style="text-align: center;">发布新课程</h2>
    <el-steps :active="2" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
       <el-step title="试卷列表"/>
      <el-step title="最终发布"/>
    </el-steps>
    <el-button type="text" @click="openChapterDialog()">添加章节</el-button>

    <!-- 章节 -->
   
    <ul class="chanpterList">
      <el-collapse accordion>
      <li
        v-for="(chapter,index) in chapterVideoList"
        :key="chapter.id">
  <el-collapse-item>
    <template slot="title">
     <p>
  第{{ index+1}}章节：{{ chapter.title }}
     
          <span class="acts">
<!--                    <el-button style="" type="text" @click="openVideo(chapter.id)">添加小节</el-button>-->
                 <!-- <el-button style="" @click="openQuestionDialog(chapter.id)" type="text">试题列表</el-button> -->
                  <el-button style="" @click="openTypedialog(chapter.id)" type="text">添加试题类型</el-button>
                  <el-button style="" type="text" @click="openEditChatper(chapter.id)">编辑</el-button>
                  <el-button type="text" @click="removeChapter(chapter.id)">删除</el-button>
       </span>
        </p>
    </template>
      <ul class="chanpterList videoList ">
         <li
            v-for="type in chapter.children" 
           :key="type.id">
            <p>
            {{ type.questionTypeSysName }}
              <span class="acts">
                    <el-button style="" @click="openQuestionDialog(type)"  type="text" >查看试题列表</el-button>
                    <el-button type="text" @click="removetype(type.id)">删除</el-button>
              </span>   
            </p>     
          </li>
        </ul>
  </el-collapse-item>
      </li>
      </el-collapse>
    </ul>
    <div>
      <el-button @click="previous">上一步</el-button>
      <el-button :disabled="saveBtnDisabled" type="primary" @click="next">下一步</el-button>
    </div>

    <!-- 添加和修改试题表单 -->
        <el-dialog :visible.sync="dialogChoiceQuestionForm" title="添加选择试题"
               v-if="dialogChoiceQuestionForm">
      <el-divider></el-divider>
        <el-form :model="questionChoice" label-width="120px" >
          <el-form-item label="时长/分钟">
            <el-input-number v-model="questionChoice.time" :min="0" controls-position="right"/>
          </el-form-item>
  
          <el-form-item label="难度">
            <el-input-number v-model="questionChoice.difficulty" :min="1" :max="5" ></el-input-number>
          </el-form-item>
          <el-form-item label="题干">
            <el-input type="textarea" v-model="questionChoice.content"></el-input>
          </el-form-item>
          <el-form-item label="选项A">
            <el-input  v-model="questionChoice.choiceA"></el-input>
          </el-form-item>
          <el-form-item label="选项B">
            <el-input  v-model="questionChoice.choiceB"></el-input>
          </el-form-item>
          <el-form-item label="选项C">
            <el-input  v-model="questionChoice.choiceC"></el-input>
          </el-form-item>
          <el-form-item label="选项D">
            <el-input  v-model="questionChoice.choiceD"></el-input>
          </el-form-item>
          <el-form-item label="答案">
            <el-radio-group v-model="questionChoice.answer">
              <el-radio label="A" ></el-radio>
              <el-radio label="B" ></el-radio>
              <el-radio label="C" ></el-radio>
              <el-radio label="D"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="questionChoice.title"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogChoiceQuestionForm = false">取 消</el-button>
          <el-button type="primary" @click="saveOrUpdateChoiceQuestion()">确 定</el-button>
        </div>
   
    </el-dialog>
    <el-dialog :visible.sync="dialogQuestionForm" title="添加试题"
               v-if="dialogQuestionForm">
       <el-divider></el-divider>
        <el-form :model="question" label-width="120px">
          <el-form-item label="类型">
            <el-select v-model="question.type"  >
              <el-option
                v-for="item in options"
                :key="item.questionTypeSysId"
                :label="item.questionTypeSysName"
                :value="item.questionTypeSysId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="时长/分钟">
            <el-input-number v-model="question.time" :min="0" controls-position="right"/>
          </el-form-item>
          <el-form-item label="排序">
            <el-input-number v-model="question.sort" :min="0" controls-position="right"/>
          </el-form-item>
          <el-form-item label="难度">
            <el-input-number v-model="question.difficulty" :min="1" :max="5" ></el-input-number>
          </el-form-item>
          <el-form-item label="题干">
            <el-input type="textarea" v-model="question.content"></el-input>
          </el-form-item>
          <el-form-item label="答案">
            <el-input type="textarea" v-model="question.answer"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="question.title"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogQuestionForm = false">取 消</el-button>
          <el-button type="primary" @click="saveOrUpdateQuestion()">确 定</el-button>
        </div>
    
    
      

    </el-dialog>
    <!-- 添加和修改章节表单 -->
    <el-dialog :visible.sync="dialogChapterFormVisible" title="添加章节">
      <el-form :model="chapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="chapter.title"/>
        </el-form-item>
        <!-- <el-form-item label="章节排序">
          <el-input-number v-model="chapter.sort" :min="0" controls-position="right"/>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChapterFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="openUpload"  v-if="openUpload" title="上传excel" >
        <el-form label-width="120px">
          <el-form-item label="信息描述">
            <el-tag type="info">普通类型试题excel模板</el-tag>
            <el-tag>
              <i class="el-icon-download"/>
              <a :href="'/static/uploadquestion.xlsx'">点击下载模版</a>
            </el-tag>
          </el-form-item>
          <el-form-item label="选择Excel">
            <el-upload
              v-if="openUpload"
              ref="upload"
              :auto-upload="false"
              :on-success="fileUploadSuccess"
              :on-error="fileUploadError"
              :disabled="importBtnDisabled"
              :limit="1"
              :data="QuestionUploadVo"
              :action="'http://localhost:9001/eduservice/edu_question/upload'"
              name="file"
              accept="application/vnd.ms-excel">
              <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
              <el-button
                :loading="loading"
                style="margin-left: 10px;"
                size="small"
                type="success"
                @click="submitUpload">上传到服务器</el-button>
            </el-upload>
          </el-form-item>
        </el-form>
    
    
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="clearfile">清除</el-button>
        <el-button @click="openUpload=false">取 消</el-button>
      </div>
    </el-dialog>
     <el-dialog :visible.sync="openUploadChoice"  v-if="openUploadChoice" title="选择题上传" >
        <el-form label-width="120px">
          <el-form-item label="信息描述">
            <el-tag type="info">选择题excel模板</el-tag>
            <el-tag>
              <i class="el-icon-download"/>
              <a :href="'/static/uploadquestionChoice.xlsx'">点击下载模版</a>
            </el-tag>
          </el-form-item>
          <el-form-item label="选择Excel">
            <el-upload
              ref="upload"
              :auto-upload="false"
              :on-success="fileUploadSuccess"
              :on-error="fileUploadError"
              :disabled="importBtnDisabled"
              :limit="1"
              :data="QuestionUploadVo"
              :action="'http://localhost:9001/eduservice/edu_question_choice/upload'"
              name="file"
              accept="application/vnd.ms-excel">
              <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
              <el-button
                :loading="loading"
                style="margin-left: 10px;"
                size="small"
                type="success"
                @click="submitUpload">上传到服务器</el-button>
            </el-upload>
          </el-form-item>
        </el-form>
    
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="clearfile">清除</el-button>
        <el-button @click="openUploadChoice=false">取 消</el-button>
      </div>
    </el-dialog>
<el-dialog :visible.sync="openWordUpload"  v-if="openWordUpload" title="上传word解析习题" >
  <el-form label-width="120px">
    <el-form-item label="信息描述">
      <el-tag type="info">word模版说明</el-tag>
      <el-tag>
        <i class="el-icon-download"/>
        <a :href="'/static/01.xlsx'">点击下载模版</a>
      </el-tag>
    </el-form-item>
    <el-form-item label="选择要解析的word">
      <el-upload
        ref="uploadword"
        :auto-upload="false"
        :on-success="fileWordUploadSuccess"
        :on-error="fileUploadError"
        :limit="1"
        :data="QuestionUploadVo"
        :action="BASE_API+'/eduservice/edu_upload_word/userWordUpload'"
        name="file"
     >
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        <el-button
          :loading="loading"
          style="margin-left: 10px;"
          size="small"
          type="success"
          @click="submitWordUpload">上传到服务器</el-button>
      </el-upload>
    </el-form-item>
  </el-form>
</el-dialog>
<el-dialog :visible.sync="dialogTypeForm"   title="新增试题类型" >
  <el-form label-width="120px">
     <el-form-item label="类型">
            <el-select v-model="typeObj.questionTypeSysId"  >
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
       <el-button
          :loading="loading"
          style="margin-left: 10px;"
          size="small"
          type="success"
          @click="addType()">添加试题类型到章节</el-button>
  </el-form>
  
</el-dialog>
<el-dialog :visible.sync="dialogQuestionChoice"   width="95%"  he title="选择试题列表" >
    <el-button @click="openChoiceQuestionForm()">添加试题</el-button>
      <el-button @click="openUploadChoice=!openUploadChoice">以excel形式上传</el-button>
      <el-button @click="openWordUpload=!openWordUpload">上传word解析习题</el-button>
  <el-table
        :data="choiceList"
        style="width: 100%"
        >
        
      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (choicepage - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
        <el-table-column
          prop="content"
          label="题干"
          style=""
          min-width="300">
        </el-table-column>
        <el-table-column
          prop="answer"
          label="答案"
          width="100">
        </el-table-column>
         <el-table-column
          prop="choiceA"
          label="选项A"
          width="150">
        </el-table-column>
             <el-table-column
          prop="choiceB"
          label="选项B"
          width="150">
        </el-table-column>
  
             <el-table-column
          prop="choiceC"
          label="选项C"
          width="150">
        </el-table-column>
                   <el-table-column
          prop="choiceD"
          label="选项D"
          width="150">
        </el-table-column>
        <el-table-column
          prop="difficulty"
          label="难度"
          width="90">
        </el-table-column>
        <el-table-column
          prop="useCount"
          label="引用次数"
          width="90">

        </el-table-column>
        <el-table-column
          prop="title"
          label="名称"
         >
        </el-table-column>
        <el-table-column label="操作"  align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteChoiceQuestion(scope.row.typeid)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="this.choicepage"
      :page-size="limit"
      :total="this.choicetotal"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchChoiceQuestion"
    />
  
</el-dialog>
    <el-dialog :visible.sync="dialogQuestion" title="试题列表" width="95%">
      <el-button @click="openQuestionFromDialog()">添加试题</el-button>
      <el-button @click="openUpload=!openUpload">以excel形式上传</el-button>
      <el-button @click="openWordUpload=!openWordUpload">上传word解析习题</el-button>
      <el-table
        :data="questions"
        style="width: 90%"
        >
        
      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (quesitonpage - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
        <el-table-column
          prop="content"
          label="题干"
          style=""
          min-width="400">
        </el-table-column>
        <el-table-column
          prop="answer"
          label="答案"
          width="330">
        </el-table-column>
        <el-table-column
          prop="difficulty"
          label="难度"
          width="90">
        </el-table-column>
        <el-table-column
          prop="useCount"
          label="引用次数"
          width="90">

        </el-table-column>
        <el-table-column
          prop="type"
          label="类型"
          width="90">
        </el-table-column>
        <el-table-column
          prop="title"
          label="名称"
          width="100">
        </el-table-column>
        <el-table-column label="操作"  align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteQuestion(scope.row.typeid)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
          <!-- 分页 -->
    <el-pagination
      :current-page="this.quesitonpage"
      :page-size="limit"
      :total="this.questiontotal"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchQuestion"
    />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogQuestion = false">关闭</el-button>
       </div>
    </el-dialog>
    <!-- 添加和修改课时表单 -->
<!--    <el-dialog :visible.sync="dialogVideoFormVisible" title="添加课时">-->
<!--      <el-form :model="video" label-width="120px">-->
<!--        <el-form-item label="课时标题">-->
<!--          <el-input v-model="video.title"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="课时排序">-->
<!--          <el-input-number v-model="video.sort" :min="0" controls-position="right"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否免费">-->
<!--          <el-radio-group v-model="video.free">-->
<!--            <el-radio :label="true">免费</el-radio>-->
<!--            <el-radio :label="false">默认</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->

<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button @click="dialogVideoFormVisible = false">取 消</el-button>-->
<!--        <el-button :disabled="saveVideoBtnDisabled" type="primary" @click="saveOrUpdateVideo">确 定</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->

<!--    <el-dialog :visible.sync="dialogPaper" title="试卷添加" width="80%">-->
<!--      <el-form :model="paper" label-width="90px">-->
<!--        <el-form-item label="课时标题">-->
<!--          <el-input v-model="paper.title"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="时长/分钟">-->
<!--          <el-input-number v-model="paper.time"  :min="1" ></el-input-number>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="难度">-->
<!--          <el-rate-->
<!--          v-model="paper.difficulty"-->
<!--            :texts="diff"-->
<!--            :colors="['#99A9BF', '#F7BA2A', '#F56c6c']"-->
<!--            show-text-->
<!--          >-->
<!--          </el-rate>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="排序">-->
<!--          <el-input-number v-model="paper.sort" :min="0" controls-position="right"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="内容">-->
<!--          <tinymce  v-model="paper.content"/>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button @click="dialogPaper = false">取 消</el-button>-->
<!--        <el-button :disabled="savePaperBtnDisabled" type="primary" @click="saveOrUpdatePaper">确 定</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
<!--    <el-dialog :visible.sync="dialogPaperAnswer" title="试卷答案添加" width="80%">-->
<!--      <el-form :model="paperAnswer" label-width="90px">-->
<!--        <el-form-item label="排序">-->
<!--          <el-input-number v-model="paperAnswer.sort" :min="0" controls-position="right"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="内容">-->
<!--          <tinymce  v-model="paperAnswer.content"/>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button @click="dialogPaperAnswer = false">取 消</el-button>-->
<!--        <el-button :disabled="savePaperAnswerBtnDisabled" type="primary" @click="saveOrUpdatePaperAnswer">确 定</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
  </div>
</template>
<script>
import questiontype from 'api/questiontype'
import chapter from 'api/edu/chapter'
//import video from '@/api/edu/video'
//import Tinymce from '@/components/Tinymce'
import papere from "api/edu/paper";
import question from "api/question";
import {mapActions, mapGetters} from 'vuex';
export default {
  //components:{Tinymce},
  components:{
    info_course: () => import('views/Course/publish_course/info.vue'),
    paperList: () => import('views/Course/publish_course/paper.vue'),
  
  },
       watch: {
      isFollow (newVal, oldVal) {
        this.courseId=newVal
      this.getChapterVideo()
      }
    },
 computed: {
     isFollow () {
        return this.$store.state.course.currentCourseId ;　　//需要监听的数据
      },
    ...mapGetters([ // https://vuex.vuejs.org/zh-cn/getters.html
      'getCourseCurrentTab',
      'getCourseTabs'
    ])
  },

  data() {
    return {
      questiontotal: 0, // 总记录数
      quesitonpage: 1, // 页码
      limit: 10, 
      choicetotal: 0, // 总记录数
      choicepage: 1, // 页码
      dialogChoiceQuestionForm:false,
   
      typeList:[],
      dialogQuestionChoice:false,
        dialogTypeForm:false,
       
       nextItem: {
          "menuId": "course_33",
          "menuNameCn":"试卷列表",
        
          "iconCls": "el-icon-location",
          "children":[],
          "comp":"paperList",
          "url":"/paperList"
      },
      preItem:{
          "menuId": "course_3",
          "menuNameCn":"新建课程",
        
          "iconCls": "el-icon-location",
          "children":[],
          "comp":"info_course",
          "url":"/info_course"
      },
      uploadtype:'切换为上传普通类型试题',
      isChoiceQuestion:false,
      QuestionUploadVo:{
        typeId:'',
        sysTypeId:''
      },
      BASE_API: process.env.BASE_API, // 接口API地址
      importBtnDisabled: false, // 按钮是否禁用,
      loading: false,
      isChoice:true,
      options: [],
      diff:["无聊","容易","中等","费时","极难"],
      savePaperAnswerBtnDisabled:false,
      savePaperBtnDisabled:false,
      saveBtnDisabled:false,
      openUpload:false,
           openUploadChoice:false,
      openWordUpload:false,
    //  saveVideoBtnDisabled:false,
      courseId:'',//课程id
      chapterVideoList:[],
      choiceList:[],
      questions:[],
      chapter:{ //封装章节数据
        title: '',
        sort: 0
      },
      // video: {
      //   title: '',
      //   sort: 0,
      //   free: 0,
      //   videoSourceId: ''
      // },
      //  paperflag:'',
      // paperAnswer:{
      //   id:'',
      //   content:'',
      //   sort:'',
      // },
      // paper:{
      //   id:'',
      //   title:'',
      //   courseId:'',
      //   type:'',
      // //  videoId:'',
      //   time:Number,
      //   difficulty:1,
      //   sort:0,
      //   useCount:'',
      //   content:'',
      //   ishaveanswer:''
      // },
      typeId:'',
      typeSys:'',
  //    videoId:'',
      question:{
        id:'',
        title:'',
        content:'',
        answer:'',
    type:'',
     //   videoId:'',
        time:'',
        difficulty:1,
        sort:0,
        useCount:0,
      },
      questionChoice:{
        id:'',
        title:'',
        content:'',
        choiceA:'',
        choiceB:'',
        choiceC:'',
        choiceD:'',
        chapterId:'',
        answer:'',
        time:'',
        difficulty:1,
        sort:0,
        useCount:0,
      },
      typeObj:{
        chapterId:'',
        questionTypeSysId:'',
        courseId:'',      
      },
      typeAndQuestion:{
registId:'',
questionId:'',
      },
      dialogQuestion:false,
      dialogQuestionForm:false,//章节弹框
      dialogChapterFormVisible:false,//章节弹框
    //  dialogVideoFormVisible:false, //小节弹框
      dialogPaper:false,
      dialogPaperAnswer:false,
      searchObj:{
          typeId:'',
          courseId:'',
    },
    maxChapter:1,
    }
  },
  created() {
    //获取路由的id值

    if(this.$store.state.course.currentCourseId!='none') {

      this.courseId = this.$store.state.course.currentCourseId
    
      //根据课程id查询章节和小节
      this.getChapterVideo()
   
     
    }
    this.getTypeList()
  },
  methods:{
    ...mapActions([
      'courseAddTab',
       'courseRemoveTab',
    ]),
 
    getTypeList(){
      if(this.typeList[0]==null){
  questiontype.getSysType().then(res=>{
       
        this.typeList=res.data.data.list

      })
      }
    
    },
    clearfile(){
      this.$refs.upload.clearFiles()


    },
    // isChoiceButton(){
    //   this.isChoiceQuestion=!this.isChoiceQuestion
    //   if(this.isChoiceQuestion==false){
    //     this.uploadtype='切换为上传普通类型试题'
    //   }else{
    //     this.uploadtype='切换为上传选择题'
    //   }
    // },
    submitWordUpload(){

      this.importBtnDisabled = true
      this.loading = true
      // js: document.getElementById("upload").submit()
      this.$refs.uploadword.submit()
      this.QuestionUploadVo={}
    },
    submitUpload() {
      this.QuestionUploadVo.sysTypeId=this.typeSys
      this.QuestionUploadVo.typeId=this.typeId
      this.importBtnDisabled = true
      this.loading = true
      // js: document.getElementById("upload").submit()
      this.$refs.upload.submit()
      this.QuestionUploadVo={}
    },
    //上传成功
    fileWordUploadSuccess(){
      this.loading = false
      this.$message({
        type: 'success',
        message: '上传word成功'
      })
      //跳转课程分类列表
      //路由跳转
      this.openWordUpload=false
      this.$refs.uploadword.clearFiles()
     // question.getQuestios(this.chapterId)
    },
    fileUploadSuccess(response) {
      //提示信息
      this.loading = false
      this.$message({
        type: 'success',
        message: '上传习题成功'
      })
      //跳转课程分类列表
      //路由跳转
      this.openUpload=false
      this.clearfile()
     this.fetchQuestion(this.quesitonpage)
    },
    //上传失败
    fileUploadError() {
      this.loading = false
      this.$message({
        type: 'error',
        message: '上传文件失败'
      })
    },
     ChoicefileUploadSuccess(response) {
      //提示信息
      this.loading = false
      this.$message({
        type: 'success',
        message: '上传习题成功'
      })
      //跳转课程分类列表
      //路由跳转
      this.openUpload=false
      this.clearfile()
      this.fetchChoiceQuestion(this.choicepage) 
    },
    //上传失败
    fileUploadError() {
      this.loading = false
      this.$message({
        type: 'error',
        message: '上传文件失败'
      })
    },
  
 
  
    deleteQuestion(id){
      this.$confirm('此操作将删除试题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {  //点击确定，执行then方法
        //调用删除的方法
        question.deleteQuestionAndMid(id)
          .then(response =>{//删除成功
            //提示信息
            this.$message({
              type: 'success',
              message: '删除试题成功!'
            });
            //刷新页面
         this.openUpload=false
         
this.fetchQuestion(this.quesitonpage)       
            // this.getQuestions(this.chapterId)
          })
      }) //点击取消，执行catch方法

    },
    
    deleteChoiceQuestion(id){
      this.$confirm('此操作将删除试题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {  //点击确定，执行then方法
        //调用删除的方法
 
        question.deleteChoiceQuestionAndMid(id)
          .then(response =>{//删除成功
            //提示信息
            this.$message({
              type: 'success',
              message: '删除试题成功!'
            });
            //刷新页面
          this.openUploadChoice=false
this.fetchChoiceQuestion(this.choicepage)       
            // this.getQuestions(this.chapterId)
           
          })
      }) //点击取消，执行catch方法

    },
       getQuestionsByTypeId(id){
         this.searchObj.typeId=id
        this.fetchQuestion()
    },
    fetchQuestion(page=1){
      this.quesitonpage=page
  question.getQuestionsByTypeId(this.quesitonpage,10,this.searchObj).then(res=>{
        this.questions=res.data.data.list
        this.questiontotal = res.data.data.total
      })
    },
     fetchChoiceQuestion(page=1){
       this.choicepage=page
  question.getChoiceQuestionsByTypeId(this.choicepage,10,this.searchObj).then(res=>{
        this.choiceList=res.data.data.list
       this.choicetotal = res.data.data.total
        
      })
    },
        getChoiceQuestionsByTypeId(id){
             this.searchObj.typeId=id
    this.fetchChoiceQuestion()
    },
       addChoiceQuestion(){
        question.addChoiceQuestion(this.questionChoice).then(res=>{
            this.typeAndQuestion.questionId=res.data.data.id
        this.typeAndQuestion.registId=this.typeId
          questiontype.addChoiceQuestionAndType(this.typeAndQuestion).then(res=>{
            if (res.data.code==20000){
                  this.$message({
          type: 'success',
          message: '添加试题成功!'
             });
            }
          })
        this.getChoiceQuestionsByTypeId( this.typeId)
          this.dialogChoiceQuestionForm=false
          this.questionChoice={}
       
          
        })
    },
    addQuestion(){
      question.addQuestion(this.question).then(res=>{
        
        this.typeAndQuestion.questionId=res.data.data.id
        this.typeAndQuestion.registId=this.typeId
         questiontype.addQuestionAndType(this.typeAndQuestion).then(res=>{
               if (res.data.code==20000){
                  this.$message({
          type: 'success',
          message: '添加试题成功!'
             });
      }
         })
        this.question={}
        this.typeAndQuestion={}
        
         this.getQuestionsByTypeId(this.typeId)
         this.dialogQuestionForm=false
        // this.getQuestions(this.chapterId)
      })
    },
      updateQuestion(){

    },
      updateChoiceQuestion(){

    },
    openQuestionDialog(type){
    this.typeSys=type.questionTypeSysId
      this.options=[]
      this.options[0]=type
      question.type=type.id
      this.typeId=type.id
    
     if(type.questionTypeSysId==1){
       this.dialogQuestionChoice=true
         this.getChoiceQuestionsByTypeId(this.typeId)

     }else{
        this.dialogQuestion=true
       this.getQuestionsByTypeId(this.typeId)
     }
    },
   
    openQuestionFromDialog(videoId){
      
      this.dialogQuestionForm=true
  
    },
   
    openChoiceQuestionForm(){
      this.dialogChoiceQuestionForm=true
    
    },
    // getQuestions(chapterId){
    //   question.getQuestios(chapterId).then(res=>{
    //     this.questions=res.data.data.questions

    //   })
    // },
    openTypedialog(chapterId){
        this.typeObj.chapterId=chapterId
        this.dialogTypeForm=true
    },
    addType(){
      this.typeObj.courseId=this.courseId
     
      questiontype.addType(this.typeObj)
      this.dialogTypeForm=false
        this.getChapterVideo()
    },
       saveOrUpdateQuestion(){
      if(!this.question.id){
        this.addQuestion()
      }else {
        this.updateQuestion()
      }
    },
      saveOrUpdateChoiceQuestion(){
      if(!this.questionChoice.id){
        this.addChoiceQuestion()
      }else {
        this.updateChoiceQuestion()
      }
    },
///////////////////////////////////////////////////////////////
//     addPaper(){
//       this.paper.courseId=this.courseId
//
//       papere.addPaper(this.paper).then(res=>{
//         this.dialogPaper = false
//         //提示
//         this.$message({
//           type: 'success',
//           message: '添加试卷成功!'
//         });
//         //刷新页面
//         this.getChapterVideo()
//       })
//     },
//     addPaperAnswer(){
//
//       papere.addPaperAnswer(this.paperAnswer).then(res=>{
//         this.dialogPaperAnswer=false
//         this.$message({
//           type: 'success',
//           message: '添加试卷答案成功!'
//         });
//         //刷新页面
//         this.getChapterVideo()
//       })
//     },
//     removePaper(id){
//       this.$confirm('此操作将删除试卷, 是否继续?', '提示', {
//         confirmButtonText: '确定',
//         cancelButtonText: '取消',
//         type: 'warning'
//       }).then(() => {  //点击确定，执行then方法
//         //调用删除的方法
//         papere.deletePaper(id)
//           .then(response =>{//删除成功
//             //提示信息
//             this.$message({
//               type: 'success',
//               message: '删除试卷成功!'
//             });
//             //刷新页面
//             this.getChapterVideo()
//           })
//       }) //点击取消，执行catch方法
//
//     },
//     removePaperAnswer(id){
//       this.$confirm('此操作将删除试卷答案, 是否继续?', '提示', {
//         confirmButtonText: '确定',
//         cancelButtonText: '取消',
//         type: 'warning'
//       }).then(() => {  //点击确定，执行then方法
//         //调用删除的方法
//         papere.deletePaperAnswer(id)
//           .then(response =>{//删除成功
//             //提示信息
//             this.$message({
//               type: 'success',
//               message: '删除试卷答案成功!'
//             });
//             //刷新页面
//             this.getChapterVideo()
//           })
//       }) //点击取消，执行catch方法
//
//     },
//     updataPaper(){
//       papere.updatePaper(this.paper).then(res=>{
//         this.dialogPaper = false
//         //提示
//         this.$message({
//           type: 'success',
//           message: '修改试卷成功!'
//         });
//         //刷新页面
//         this.getChapterVideo()
//       })
//     },
//     updataPaperAnswer(){
//       papere.updatePaperAnswer(this.paperAnswer).then(res=>{
//         this.dialogPaperAnswer = false
//         //提示
//         this.$message({
//           type: 'success',
//           message: '修改试卷答案成功!'
//         });
//         //刷新页面
//         this.getChapterVideo()
//       })
//     },
//     openPaperDigo(id){
//       this.dialogPaper=true
//
//       this.paper.videoId=id
//
//     },
//     openPaperAnswerDigo(id){
//       this.paperAnswer.id=id
//       this.dialogPaperAnswer=true
//     },
//     openPaperEditDigo(paperId,id){
//       this.dialogPaper=true
//       this.paper.videoId=id
//       papere.getPaper(paperId).then(res=>{
//         this.paper=res.data.paper
//
//       })
//     },
//     openPaperAnswerEditDigo(paperId){
//       this.dialogPaperAnswer=true
//
//       papere.getPaperAnswer(paperId).then(res=>{
//         this.paperAnswer=res.data.paper
//         this.paperflag=1
//       })
//     },
//     saveOrUpdatePaper() {
//       if(!this.paper.id){
//         this.addPaper()
//       }else {
//         this.updataPaper()
//       }
//     },
//     saveOrUpdatePaperAnswer() {
//       if(this.paperAnswer.id){
//         this.addPaperAnswer()
//       }else if(this.paperflag){
//         this.updataPaperAnswer()
//       }
//     },

//==============================小节操作====================================
//     //删除小节
//     removeVideo(id) {
//       this.$confirm('此操作将删除小节, 是否继续?', '提示', {
//         confirmButtonText: '确定',
//         cancelButtonText: '取消',
//         type: 'warning'
//       }).then(() => {  //点击确定，执行then方法
//         //调用删除的方法
//         video.deleteVideo(id)
//           .then(response =>{//删除成功
//             //提示信息
//             this.$message({
//               type: 'success',
//               message: '删除小节成功!'
//             });
//             //刷新页面
//             this.getChapterVideo()
//           })
//       }) //点击取消，执行catch方法
//     },
//     //添加小节弹框的方法
//     openVideo(chapterId) {
//       //弹框
//       this.dialogVideoFormVisible = true
//       //设置章节id
//       this.video.chapterId = chapterId
//     },
//     //添加小节
//     addVideo() {
//       //设置课程id
//       this.video.courseId = this.courseId
//       video.addVideo(this.video)
//         .then(response => {
//           //关闭弹框
//           this.dialogVideoFormVisible = false
//           //提示
//           this.$message({
//             type: 'success',
//             message: '添加小节成功!'
//           });
//           //刷新页面
//           this.getChapterVideo()
//         })
//     },
//     saveOrUpdateVideo() {
//       this.addVideo()
//     },

//==============================章节操作====================================
    //删除章节
    removeChapter(chapterId) {
      this.$confirm('此操作将删除章节, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {  //点击确定，执行then方法
        //调用删除的方法
        chapter.deleteChapter(chapterId)
          .then(response =>{//删除成功
            //提示信息
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            //刷新页面
            this.getChapterVideo()
          })
      }) //点击取消，执行catch方法
    },
    //修改章节弹框数据回显
    openEditChatper(chapterId) {
      //弹框
      this.dialogChapterFormVisible = true
      //调用接口
      chapter.getByChapterById(chapterId)
        .then(response => {
          this.chapter = response.data.data.chapter
        })
    },
    //弹出添加章节页面
    openChapterDialog() {
      //弹框
      this.dialogChapterFormVisible = true
      //表单数据清空
      this.chapter.title = ''
      this.chapter.sort = 0
    },
    //添加章节
    addChapter() {
      //设置课程id到chapter对象里面
     
      this.chapter.courseId = this.courseId
     this.chapter.sort=this.maxChapter++
      chapter.addChapter(this.chapter)
        .then(response => {
          //关闭弹框
          this.dialogChapterFormVisible = false
          //提示
          this.$message({
            type: 'success',
            message: '添加章节成功!'
          });
          //刷新页面
          this.getChapterVideo()
        })
    },
    //修改章节的方法
    updateChapter() {
      chapter.updateChapter(this.chapter)
        .then(response =>  {
          //关闭弹框
          this.dialogChapterFormVisible = false
          //提示
          this.$message({
            type: 'success',
            message: '修改章节成功!'
          });
          //刷新页面
          this.getChapterVideo()
        })
    },
    saveOrUpdate() {
      if(!this.chapter.id) {
        this.addChapter()
      } else {
        this.updateChapter()
      }
    },
    //根据课程id查询章节和小节
    getChapterVideo() {
     
      chapter.getNestedTreeList(this.courseId)
        .then(response => {
          this.chapterVideoList = response.data.data.chapters
          this.maxChapter=this.chapterVideoList.length+1
        
        })
    },
    previous() {
    
           this.$store.state.course.CurrentCourseId=this.courseId
          this.courseAddTab(this.preItem)
          this.courseRemoveTab("course_31")
    },
    next() {
      //跳转到第二步
 
         this.$store.state.course.currentCourseId=this.courseId
          this.courseAddTab(this.nextItem)
          this.courseRemoveTab("course_31")
          
    }
  }
}
</script>
<style scoped>
.chanpterList{
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chanpterList li{
  position: relative;
}
.chanpterList p{
  float: left;
  font-size: 25px;
  margin: 25px 3;
  padding: 10px;
  height: 25px;
  line-height: 30px;
  width: 90%;
  border: 1px solid #DDD;
}
.chanpterList .acts {
  float: right;
  font-size: 20px;
}

.videoList{
  padding-left: 80px;
}
.videoList p{
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 40px;
  line-height: 30px;
  width: 85%;
  border: 1px dotted #DDD;
}
.paperList{
  padding-left: 100px;

}
.paperList p{
  height: 55px;
}

</style>
